//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <elasys_server.h>
#include <winhack.h>
#include "notifiercenter.h"

CNotifierCenter g_notifierCenter;

CNotifierCenter::CNotifierCenter(Int32 capacity)
                                : m_hook(capacity)
{
    //InitializeCriticalSection(&m_hLock);
}

CNotifierCenter::~CNotifierCenter()
{
   // DeleteCriticalSection(&m_hLock);
}

ECode CNotifierCenter::Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 uNumberOfBytesToRead,
        /* [out] */ MemoryBuf * pData,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    assert(0);
    return E_NOT_IMPLEMENTED;
}

ECode CNotifierCenter::Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & ebbData,
        /* [out] */ Int32 * puNumberOfBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    assert(0);
    return E_NOT_IMPLEMENTED;
}

ECode CNotifierCenter::Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & ebbInData,
        /* [out] */ MemoryBuf * pOutData,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    Int32 type = nControlCode & 0xffff;
    Int32 code = nControlCode >> 16;

    m_hook.CallNext((int)type, (int)code, ebbInData.GetPayload());

    return NOERROR;
}

ECode CNotifierCenter::AddNotifier(
            /* [in] */ Int32 type,
            /* [in] */ const PVoid pNotifierProc)
{
    int res = -1;

    res = m_hook.Set((int)type, pNotifierProc);
    if (res < 0) {
        return E_INVALID_ARGUMENT;
    }
    return NOERROR;
}

void CNotifierCenter::CallNotifier(
            /* [in] */ Int32 type,
            /* [in] */ Int32 code,
            /* [in] */ PVoid  pParam)
{
    m_hook.CallNext((int)type, (int)code, pParam);
}

void CNotifierCenter::RemoveNotifier(
            /* [in] */ Int32 type)
{
    m_hook.Set((int)type, NULL);
}

